package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskTypeDAO;

import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskType;

import cn.edu.scau.cmi.hombio.service.TaskTypeService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for TaskType entities
 * 
 */

@Controller("TaskTypeRestController")

public class TaskTypeRestController {

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskType entities
	 * 
	 */
	@Autowired
	private TaskTypeDAO taskTypeDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for TaskType entities
	 * 
	 */
	@Autowired
	private TaskTypeService taskTypeService;

	/**
	 * Save an existing TaskType entity
	 * 
	 */
	@RequestMapping(value = "/TaskType", method = RequestMethod.PUT)
	@ResponseBody
	public TaskType saveTaskType(@RequestBody TaskType tasktype) {
		taskTypeService.saveTaskType(tasktype);
		return taskTypeDAO.findTaskTypeByPrimaryKey(tasktype.getId());
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* View an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskType/{tasktype_id}/tasks/{task_id}", method = RequestMethod.GET)
	@ResponseBody
	public Task loadTaskTypeTasks(@PathVariable Integer tasktype_id, @PathVariable Integer related_tasks_id) {
		Task task = taskDAO.findTaskByPrimaryKey(related_tasks_id, -1, -1);

		return task;
	}

	/**
	* Delete an existing TaskType entity
	* 
	*/
	@RequestMapping(value = "/TaskType/{tasktype_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskType(@PathVariable Integer tasktype_id) {
		TaskType tasktype = taskTypeDAO.findTaskTypeByPrimaryKey(tasktype_id);
		taskTypeService.deleteTaskType(tasktype);
	}

	/**
	* Save an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskType/{tasktype_id}/tasks", method = RequestMethod.PUT)
	@ResponseBody
	public Task saveTaskTypeTasks(@PathVariable Integer tasktype_id, @RequestBody Task tasks) {
		taskTypeService.saveTaskTypeTasks(tasktype_id, tasks);
		return taskDAO.findTaskByPrimaryKey(tasks.getId());
	}

	/**
	* Delete an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskType/{tasktype_id}/tasks/{task_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskTypeTasks(@PathVariable Integer tasktype_id, @PathVariable Integer related_tasks_id) {
		taskTypeService.deleteTaskTypeTasks(tasktype_id, related_tasks_id);
	}

	/**
	* Create a new Task entity
	* 
	*/
	@RequestMapping(value = "/TaskType/{tasktype_id}/tasks", method = RequestMethod.POST)
	@ResponseBody
	public Task newTaskTypeTasks(@PathVariable Integer tasktype_id, @RequestBody Task task) {
		taskTypeService.saveTaskTypeTasks(tasktype_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Show all TaskType entities
	* 
	*/
	@RequestMapping(value = "/TaskType", method = RequestMethod.GET)
	@ResponseBody
	public List<TaskType> listTaskTypes() {
		return new java.util.ArrayList<TaskType>(taskTypeService.loadTaskTypes());
	}

	/**
	* Select an existing TaskType entity
	* 
	*/
	@RequestMapping(value = "/TaskType/{tasktype_id}", method = RequestMethod.GET)
	@ResponseBody
	public TaskType loadTaskType(@PathVariable Integer tasktype_id) {
		return taskTypeDAO.findTaskTypeByPrimaryKey(tasktype_id);
	}

	/**
	* Show all Task entities by TaskType
	* 
	*/
	@RequestMapping(value = "/TaskType/{tasktype_id}/tasks", method = RequestMethod.GET)
	@ResponseBody
	public List<Task> getTaskTypeTasks(@PathVariable Integer tasktype_id) {
		return new java.util.ArrayList<Task>(taskTypeDAO.findTaskTypeByPrimaryKey(tasktype_id).getTasks());
	}

	/**
	* Create a new TaskType entity
	* 
	*/
	@RequestMapping(value = "/TaskType", method = RequestMethod.POST)
	@ResponseBody
	public TaskType newTaskType(@RequestBody TaskType tasktype) {
		taskTypeService.saveTaskType(tasktype);
		return taskTypeDAO.findTaskTypeByPrimaryKey(tasktype.getId());
	}
}